package tt.dz.order.pojo;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import tt.dz.order.dao.BaseDAOUtil;
import tt.dz.order.exception.BusinessException;

/**
 * @Title 活动规则
 * @author zhoutian   
 * @date 2015-11-26
 */
public class ActivityRule {
	//提供服务的运营商编码（联合主键）
	private String serviceProviderCode;
	//@transient 运营商名称
	private String merchantName;
	//服务编号（联合主键）
	private String serviceNo;
	//@transient 服务名称
	private String serviceName;
	//规则名称
	private String ruleName;
	//优先级
	private Integer salience;
	//开始日期
	private String startDate;
	//结束日期
	private String endDate;
	//条件表达式
	private String conditionalExpression;
	//业务表达式
	private String bizExpression;
	//创建人
	private String creator;
	//创建时间
	private Date createTime;

	public String getServiceProviderCode() {
		return serviceProviderCode;
	}

	public void setServiceProviderCode(String serviceProviderCode) {
		this.serviceProviderCode = serviceProviderCode;
	}

	public String getMerchantName() {
		return merchantName;
	}

	public void setMerchantName(String merchantName) {
		this.merchantName = merchantName;
	}

	public String getServiceName() {
		return serviceName;
	}

	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}

	public String getServiceNo() {
		return serviceNo;
	}

	public void setServiceNo(String serviceNo) {
		this.serviceNo = serviceNo;
	}

	public String getRuleName() {
		return ruleName;
	}

	public Integer getSalience() {
		return salience;
	}

	public void setSalience(Integer salience) {
		this.salience = salience;
	}

	public void setRuleName(String ruleName) {
		this.ruleName = ruleName;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public String getConditionalExpression() {
		return conditionalExpression;
	}

	public void setConditionalExpression(String conditionalExpression) {
		this.conditionalExpression = conditionalExpression;
	}

	public String getBizExpression() {
		return bizExpression;
	}

	public void setBizExpression(String bizExpression) {
		this.bizExpression = bizExpression;
	}

	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	
	public ActivityRule() {
		
	}
	
	private static final String select_all_sql = "select serviceProviderCode, serviceNo, ruleName, salience, startDate, salience, endDate, conditionalExpression, bizExpression, creator, createTime from order_activityrule";
	
	private static final String update_all_sql = "update order_activityrule set serviceProviderCode = :serviceProviderCode, serviceNo = :serviceNo, ruleName = :ruleName, salience = :salience, startDate = :startDate, endDate = :endDate, conditionalExpression = :conditionalExpression, bizExpression = :bizExpression, creator = :creator, createTime = :createTime";
	
	public void save() throws BusinessException{
		if(isServiceNoAndServiceProviderCodeExist(this.serviceNo, this.serviceProviderCode)){
			throw new BusinessException("选择的服务和服务提供商已存在");
		}
		if(isRuleNameExist("", "", this.ruleName)){
			throw new BusinessException("规则名称重复");
		}
		String sql = "insert into order_activityrule(serviceProviderCode, serviceNo, ruleName, salience, startDate, endDate, conditionalExpression, bizExpression, creator, createTime) values(:serviceProviderCode, :serviceNo, :ruleName, :salience, :startDate, :endDate, :conditionalExpression, :bizExpression, :creator, :createTime)";
		BaseDAOUtil.updateObject(sql, this);
	}
	
	public void update() throws BusinessException{
		if(isRuleNameExist(this.serviceNo, this.serviceProviderCode, this.ruleName)){
			throw new BusinessException("规则名称重复");
		}
		String sql = update_all_sql + " where serviceNo = :serviceNo and serviceProviderCode = :serviceProviderCode";
		BaseDAOUtil.updateObject(sql, this);
	}
	
	public void delete(){
		String sql = "delete from order_activityrule where serviceNo = :serviceNo and serviceProviderCode = :serviceProviderCode";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("serviceNo", this.serviceNo);
		paramMap.put("serviceProviderCode", this.serviceProviderCode);
		BaseDAOUtil.delete(sql, paramMap);
	}
	
	public static boolean isServiceNoAndServiceProviderCodeExist(String serviceNo, String serviceProviderCode){
		ActivityRule rule = getBy(serviceNo, serviceProviderCode);
		if(rule != null){
			return true;
		}
		return false;
	}
	
	public static boolean isRuleNameExist(String serviceNo, String serviceProviderCode, String ruleName){
		String sql = select_all_sql + " where (serviceNo != :serviceNo or serviceProviderCode != :serviceProviderCode) and ruleName = :ruleName";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("serviceNo", serviceNo);
		paramMap.put("serviceProviderCode", serviceProviderCode);
		paramMap.put("ruleName", ruleName);
		List<ActivityRule> ruleList = BaseDAOUtil.queryForList(sql, paramMap, ActivityRule.class);
		if(ruleList != null && ruleList.size() > 0){
			return true;
		}
		return false;
	}
	
	/**
	 * 获取所有的活动规则
	 */
	public static List<ActivityRule> getAllRules(){
		return BaseDAOUtil.queryForList(select_all_sql, new HashMap<String, Object>(),  ActivityRule.class);
	}
	
	/**
	 * 获取所有的活动规则
	 */
	public static List<ActivityRule> getAllRulesForOperationManagement(){
		String sql = "select oa.serviceProviderCode, m.name as merchantName, oa.serviceNo, oa.ruleName, oa.salience, oa.startDate, oa.endDate, oa.conditionalExpression, oa.bizExpression, osi.name as serviceName, oa.creator, oa.createTime "
				+ " from order_activityrule oa "
				+ " inner join order_service_info osi on oa.serviceNo = osi.serviceNo "
				+ " inner join ms_merchant m on oa.serviceProviderCode = m.merchant_code "
				+ " order by oa.salience desc";
		return BaseDAOUtil.queryForList(sql, new HashMap<String, Object>(),  ActivityRule.class);
	}
	
	/**
	 * 通过服务编号、服务提供商编号查询活动规则
	 */
	public static ActivityRule getBy(String serviceNo, String serviceProviderCode){
		String sql = select_all_sql + " where serviceNo = :serviceNo and serviceProviderCode = :serviceProviderCode";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("serviceNo", serviceNo);
		paramMap.put("serviceProviderCode", serviceProviderCode);
		return BaseDAOUtil.queryForObject(sql, paramMap, ActivityRule.class);
	}
}
